package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.cszjzx.member.po.Stage;



public class StageDaoImpl implements StageDao {
	private JdbcHelper helper;
	public StageDaoImpl() throws IOException{
		helper=new JdbcHelper();
	}
	@Override
	public void add(Stage s) throws SQLException {
		String sql="insert into stage (name,id) value(?,?)";
		Object[]args={s.getName(), s.getId()};
		helper.executeUpdate(sql, args);
		
	}

	@Override
	public void delete(int id) throws SQLException {
		String sql="delete from Stage where id=?";
		Object []args={id};
		helper.executeUpdate(sql, args);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Stage> findAll() throws ClassNotFoundException, SQLException,IOException {
		String sql="select * from stage order by id";
        Object[]args={};
		return helper.executeQuery(sql, args, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
				Stage s = new Stage();
				s.setId(rs.getInt("id"));
				s.setName(rs.getString("name"));
				return s;
			}
		});	
	}
	@Override
	public void update(Stage s) throws SQLException {
		
		String sql ="UPDATE stage SET name = ? where id = ?";
		Object []args={s.getName(),s.getId()};
		helper.executeUpdate(sql, args);
	}

}
